require([ 'zTreeCheckbox', 'grid' ], function() {
	var userInfo = commonFun.execParentMethod("getUserInfo");
	var isAdminRole = userInfo.isAdminRole;
	var $fieldValueGrid = $("#fieldValueGrid");
	var $sysRoleTree = $("#sysRoleTree");

	function resize() {
		$("#left").height($(window).height());
		$("#right").height($(window).height());
		//左侧树高度
		$sysRoleTree.height($(window).height() - 55);
	}
	$(window).on('resize', resize).trigger('resize');

	initSysRoleTree();
	
	function loadAccessRes() {
		$fieldValueGrid.grid('setGridParam', {
			datatype : "json",
			postData : {
				"roleId" : $("#roleId").val(),
				"level" : $("#level").val()
			}
		}).trigger("reloadGrid");
	}

	// 设置图标
	function setNodeIcon(zNodes) {
		$.each(zNodes, function(index, item) {
			item.icon = $.getProjectName()
				+ "/content/images/role-icon_03.png";
		});
		return zNodes;
	}

	// 根据节点高亮标志，渲染树节点
	function getFontCss(treeId, treeNode) {
		return (!!treeNode.highlight) ? {
			color : "#A60000",
			"font-weight" : "bold"
		} : {
			color : "#333",
			"font-weight" : "normal"
		};
	}

	// 获得根节点并选中根节点
	function getAndSelectRootNode() {
		var node = sysRoleTreeObj.getNodesByFilter(function(node) {
			return node.level == 0;
		}, true);
		sysRoleTreeObj.selectNode(node, false);
		sysRoleTreeClick(null, node.id, node, true);
	}

	// 点击树节点
	function sysRoleTreeClick(event, treeId, treeNode, clickFlag) {
		$("#corpId").val(treeNode.corpId);
		$("#roleId").val(treeNode.id);
		$("#level").val(treeNode.level);
		// 自己的角色不允许修改
		// if (isAdminRole === "0" && $("#level").val() === "0") {
		// 	$("#saveAccessRes").hide();
		// } else {
		// 	$("#saveAccessRes").show();
		// }
		loadAccessRes();
	}

	// 获得节点数据，并且生成左侧的树
	function initSysRoleTree() {
		$.ajaxExt({
			async : true,
			url : "/sysRole/getSysRoleByName.htm"
		}).done(function(data) {
			if (!data) {
				data = [];
			}
			zNodes = setNodeIcon(data);
			// 树的参数配置
			var setting = {
				data : {
					key : {
						name : "name"
					},
					simpleData : {
						enable : true,
						idKey : "id",
						pIdKey : "parentRoleId",
						rootPId : 0
					}
				},
				view : {
					showIcon : true,
					fontCss : getFontCss
				},
				callback : {
					onClick : sysRoleTreeClick
				}
			};
			// 生成树
//			sysRoleTreeObj = $.fn.zTree.init($sysRoleTree, setting, zNodes);
			sysRoleTreeObj = $sysRoleTree.createTree(setting, data, {search: $('#search-text')});
			sysRoleTreeObj.setting.view.expandSpeed = "";
			sysRoleTreeObj.expandAll(true);
			sysRoleTreeObj.setting.view.expandSpeed = "fast";
			getAndSelectRootNode();// 选中根节点，触发事件
		});
	}

	// 获取当前角色的资源权限
	$fieldValueGrid.grid({
		url : "/sysAuthorityFunc/getCurrentAccessResData.htm",
		cmTemplate : {
			sortable : false,
			align : "left"
		},
		colModel : [ {name : "id", index : "id", label : 'id', key : true, hidden : true }, 
		    {name : "tableDsc", label : '资源模块', index : "tableDsc" }, 
		    {name : "fieldDsc", label : '资源类型', index : "fieldDsc" }, 
		    {name : "fieldCodeDsc", label : '资源属性', index : "fieldCodeDsc" } ],
		width : $(window).width() - 350,
		height : $(window).height() - 160,
		showPager:false,
		gridComplete : function() {
			// 选中其已分配的资源权限数据
			$.ajaxExt({
				async : true,
				url : "/sysAuthorityFunc/getCheckedAccessResData.htm",
				data : {
					"roleId" : $("#roleId").val()
				}
			}).done(function(data) {
				for ( var i in data) {
					$fieldValueGrid.jqGrid("setSelection", data[i].id);
				}
			});
		}
	});

	// 保存资源权限分配
	$("#saveAccessRes").on("click", function() {
		var fieldValueIds = $fieldValueGrid.jqGrid('getGridParam', 'selarrrow');
		if(fieldValueIds && fieldValueIds.length > 0){
			var loadMask = $.createMask();
			$.ajaxExt({
				url : "/sysAuthorityFunc/saveAccessResAuthority.htm",
				data : {
					"roleId":$("#roleId").val(),
					"fieldValueIds":fieldValueIds
				}
			}).done(function() {
				loadMask.remove();
				$.messager.alert("提示", "保存成功!", function() {
					loadAccessRes();
				});
			});
		}else{
			$.messager.alert("提示","请勾选至少一项资源权限。","error");
		}
	});
});
